 package ModelLayer;
import java.util.ArrayList;


public class Order
{
    
    private long orderId;
    private String regDate;
    private String delDate;
    private String status;
    private double invoiceAmount;
    private Customer orderCustomer;
    private ArrayList<OrderProducts> orderProduct;
    private Discount orderDiscount;

    public Order(long orderId, String regDate, String delDate)
    {
       orderProduct = new ArrayList<OrderProducts>();
       this.orderId = orderId;
       this.regDate = regDate; 
       this.delDate = delDate;
       this.status = "processing";
       this.invoiceAmount = 0;
    }
    
    
    public double invoiceCalculation()
    {
        invoiceAmount = 0;
     if (orderDiscount != null)
     {
        
        if(orderCustomer.getCustomerDiscount() == 0 && orderDiscount.getDiscountAmount() == 0)
        {
            for(OrderProducts op : orderProduct)
            {
                invoiceAmount += op.getSubtotal();
            }
        }
        
        if(orderCustomer.getCustomerDiscount() > 0 && orderDiscount.getDiscountAmount() == 0)
        {
            for(OrderProducts op : orderProduct)
            {
                invoiceAmount += op.getSubtotal();
            }
            invoiceAmount = invoiceAmount - ((invoiceAmount *orderCustomer.getCustomerDiscount())/100);
        }
        
        if(orderCustomer.getCustomerDiscount() == 0 && orderDiscount.getDiscountAmount() > 0)
        {
            
            for(OrderProducts op : orderProduct)
            {
                invoiceAmount += op.getSubtotal();
            }
            invoiceAmount = invoiceAmount - ((invoiceAmount *orderDiscount.getDiscountAmount())/100);
        }
        
        if(orderCustomer.getCustomerDiscount() > 0 && orderDiscount.getDiscountAmount() > 0)
        {
            double invoiceSubTotalDiscount = orderCustomer.getCustomerDiscount() + orderDiscount.getDiscountAmount();
            
            for(OrderProducts op : orderProduct)
            {
                invoiceAmount += op.getSubtotal();
            }
            
            invoiceAmount = invoiceAmount - ((invoiceAmount *invoiceSubTotalDiscount)/100);
        }
    
    }
     else
     {
         
        if(orderCustomer.getCustomerDiscount() == 0)
        {
            for(OrderProducts op : orderProduct)
            {
                invoiceAmount += op.getSubtotal();
            }
        }
        
        if(orderCustomer.getCustomerDiscount() > 0)
        {
            for(OrderProducts op : orderProduct)
            {
                invoiceAmount += op.getSubtotal();
            }
            invoiceAmount = invoiceAmount - ((invoiceAmount *orderCustomer.getCustomerDiscount())/100);
       
        }
        
        
     }
        return invoiceAmount;
        
    }

    public boolean addDiscount(Discount newOrderDiscount)
    {
        orderDiscount = newOrderDiscount;
        boolean discountAddFail = false;
        
        if((orderCustomer.getCustomerDiscount() + orderDiscount.getDiscountAmount()) > 20)
        {
        discountAddFail = true;
        orderDiscount = null;
        }
        else
        {        
          discountAddFail = false;
        }
        return discountAddFail;
    
    }
    
    public void addCustomer(Customer newOrderCustomer)
    {
        orderCustomer = newOrderCustomer;
    }
    
    public void addProduct (OrderProducts newOrderProduct)
    {
        orderProduct.add(newOrderProduct);
    }
    
    public void setRegDate(String regDate)
    {
        this.regDate =regDate;
    }
    
    public void setDelDate(String delDate)
    {
        this.delDate = delDate;
    }
    
    public void setStatus(String status)
    {
        this.status = status;
    }
    
    public void setInvoice(long invoice)
    {
        this.invoiceAmount = invoice;
    }
    
    public long getOrderId()
    {
        return orderId;
    }
    
    public String getOrderDate()
    {
        return regDate;
    }
    
    public String getDeliveryDate()
    {
        return delDate;
    }
    
    public String getStatus()
    {
        return status;
    }
    
    public double getInvoice()
    {
        return invoiceAmount;
    }
    
   
}
